package com.coolingme.demo.transparent;

import java.util.ArrayList;
import java.util.List;

/**
 * 课程包，树枝节点
 *
 * @author wangyue
 * @date 2020-09-19 0:30
 */
public class CoursePackage extends CourseComponent {

    /**
     * 子课程容器
     */
    private List<CourseComponent> items;

    /**
     * 课程名
     */
    private String name;

    /**
     * 课程等级
     */
    private Integer level;

    public CoursePackage(String name, Integer level) {
        this.items = new ArrayList<>();
        this.name = name;
        this.level = level;
    }

    @Override
    public boolean addCourse(CourseComponent component) {
        return items.add(component);
    }

    @Override
    public boolean removeCourse(CourseComponent component) {
        return items.remove(component);
    }

    @Override
    public String getName(CourseComponent component) {
        return this.name;
    }

    @Override
    public void print() {
        System.out.println(this.name);
        for (CourseComponent component : this.items) {
            // 只有树枝节点才有level
            if (this.level != null) {
                for (int i = 0; i < this.level; i++) {
                    System.out.print("    ");
                }
                for (int i = 0; i < this.level; i++) {
                    if (i == 0) {
                        System.out.print("+");
                    }
                    System.out.print("----");
                }
            }
            // 递归
            component.print();
        }
    }
}
